
using System;
using System.Threading;
using System.Collections;
using System.Collections.Generic;

public delegate void TaskHandler ();

public static class WorkingThread
{
	static Queue<TaskHandler> mWorkingQueue = new Queue<TaskHandler>();

	public static void Initialize()
	{
		Thread mainThread = new Thread(Main);
		mainThread.Start();
	}

	public static void AddTask(TaskHandler task)
	{
		lock(mWorkingQueue)
		{
			mWorkingQueue.Enqueue(task);
		}
	}

	public static void Clear()
	{
		lock(mWorkingQueue)
		{
			mWorkingQueue.Clear();
		}
	}

	static void Main()
	{
		TaskHandler task;
		while(true)
		{
			task = null;
			lock(mWorkingQueue)
			{
				if (mWorkingQueue.Count > 0)
				{
					task = mWorkingQueue.Dequeue();
				}
			}

			if (task != null)
				task();
			else
				Thread.Sleep(10);
		}

	}
}